/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.controller.dashboard;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.SMSMailUtil;
import br.com.jr2tecnologia.SMSUtil;
import br.com.jr2tecnologia.model.ClientPhonePersistenceLocal;
import br.com.jr2tecnologia.model.KeyResponsePersistenceLocal;
import br.com.jr2tecnologia.model.entity.ClientPhone;
import br.com.jr2tecnologia.model.entity.KeyResponse;
import br.com.jr2tecnologia.model.entity.Login;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;

/**
 *
 * @author ronildo
 */
@ManagedBean
@SessionScoped 
public class ClientDashboardMBean implements Serializable{
    
    private static final Logger logger = LoggerFactory.getLogger();
    
    private final Login loginInSession;

    @EJB
    private ClientPhonePersistenceLocal clientPhonePersistence;
    @EJB
    private KeyResponsePersistenceLocal keyResponsePersistence;

    private ClientPhone selectedClientPhone;
    private ClientPhone createClientPhone;
    private KeyResponse selectedKeyResponse;
    private KeyResponse createKeyResponse;
    private List<ClientPhone> listClientPhone;
    private List<KeyResponse> listKeyResponse;

    /** Creates a new instance of ClientDashboardMBean */
    public ClientDashboardMBean() {
        loginInSession = SMSMailUtil.getLoginInSession();
        selectedClientPhone = new ClientPhone();
        createClientPhone = new ClientPhone();
        selectedKeyResponse = new KeyResponse();
        createKeyResponse = new KeyResponse();
    }

    @PostConstruct
    public void initialize(){
        searchClientPhone();
        searchKeyResponse();
    }

    public List<ClientPhone> getListClientPhone() {
        return listClientPhone;
    }

    public List<KeyResponse> getListKeyResponse() {
        return listKeyResponse;
    }

    public ClientPhone getCreateClientPhone() {
        return createClientPhone;
    }

    public void setCreateClientPhone(ClientPhone createClientPhone) {
        this.createClientPhone = createClientPhone;
    }

    public KeyResponse getCreateKeyResponse() {
        return createKeyResponse;
    }

    public void setCreateKeyResponse(KeyResponse createKeyResponse) {
        this.createKeyResponse = createKeyResponse;
    }

    public ClientPhone getSelectedClientPhone() {
        return selectedClientPhone;
    }

    public void setSelectedClientPhone(ClientPhone selectedClientPhone) {
        this.selectedClientPhone = selectedClientPhone;
    }

    public KeyResponse getSelectedKeyResponse() {
        return selectedKeyResponse;
    }

    public void setSelectedKeyResponse(KeyResponse selectedKeyResponse) {
        this.selectedKeyResponse = selectedKeyResponse;
    }

    public void searchKeyResponse(){
        KeyResponse keyResponse = new KeyResponse();
        keyResponse.setLogin(loginInSession);
        listKeyResponse = keyResponsePersistence.list(keyResponse);
    }

    public void searchClientPhone(){
        ClientPhone clientPhone = new ClientPhone();
        clientPhone.setLogin(loginInSession);
        listClientPhone = clientPhonePersistence.list(clientPhone);
    }

    public void createClientPhone(){
        createClientPhone.setPkClientPhone(null);
        createClientPhone.setPhone(SMSUtil.arrangesPhone(createClientPhone.getPhone()));
        if(createClientPhone.getPhone() != null){
            createClientPhone.setCompany(loginInSession.getCompany());
            createClientPhone.setLogin(loginInSession);
            clientPhonePersistence.persist(createClientPhone);
            searchClientPhone();
            SMSMailUtil.putFacesMessage("Sucesso", "Cliente "+createClientPhone.getName()+" criado com sucesso");
        }else
            SMSMailUtil.putFacesMessage("Erro","Telefone inválido, use o seguinte exemplo: 1188047902");
    }

    public void updateClientPhone(){
        clientPhonePersistence.update(selectedClientPhone);
        searchClientPhone();
        SMSMailUtil.putFacesMessage("Sucesso", "Cliente "+selectedClientPhone.getName()+" atualizado com sucesso");
    }

    public void removeClientPhone(){
        clientPhonePersistence.remove(selectedClientPhone);
        searchClientPhone();
        SMSMailUtil.putFacesMessage("Sucesso", "Cliente "+selectedClientPhone.getName()+" removido com sucesso");
    }

    public void createKeyResponse(){
        createKeyResponse.setLogin(loginInSession);
        if(keyResponsePersistence.persist(createKeyResponse)){
            SMSMailUtil.putFacesMessage("Sucesso", "Chave "+createKeyResponse.getName()+" criada com sucesso");
            searchKeyResponse();
        }else{
            SMSMailUtil.putFacesMessage("Erro", "Esta chave "+createKeyResponse.getName()+" já esta sendo usada");
        }
        createKeyResponse = new KeyResponse();
    }

    public void updateKeyResponse(){
        keyResponsePersistence.update(selectedKeyResponse);
        SMSMailUtil.putFacesMessage("Sucesso", "Chave "+selectedKeyResponse.getName()+" atualizada com sucesso");
        searchKeyResponse();
    }

    public void removeKeyResponse(){
        try{
            keyResponsePersistence.remove(selectedKeyResponse);
            SMSMailUtil.putFacesMessage("Sucesso", "Chave "+selectedKeyResponse.getName()+" removida com sucesso");
            searchKeyResponse();
        }catch(Exception ex){
            logger.log(Level.WARNING, "Error to remove key reponse", ex);
            SMSMailUtil.putFacesMessage("Erro", "Você não tem permissão para remover esta chave");
        }
    }

    public void makeDefault(){
        KeyResponse oldDefaultKeyResponse = null, newDefaultKeyResponse = null;

        for(KeyResponse keyResponse :  listKeyResponse)
            if(keyResponse.getDefaultKey())
                oldDefaultKeyResponse = keyResponse;

        for(KeyResponse keyResponse :  listKeyResponse)
            if(keyResponse.getPkKeyResponse() == selectedKeyResponse.getPkKeyResponse())
                newDefaultKeyResponse = keyResponse;

        keyResponsePersistence.makeDefaultKeyResponse(oldDefaultKeyResponse, newDefaultKeyResponse);
        SMSMailUtil.putFacesMessage("Sucesso", "Esta chave "+selectedKeyResponse.getName()+" agora é a padrão");
        searchKeyResponse();
    }
}
